let app = getApp();
var util = require("../../utils/util.js");
var siteinfo = require('../../siteinfo.js');
Page({

  data: {
    sexList: [{
      id: 0,
      name: '请选择性别'
    }, {
      id: 1,
      name: '男'
    }, {
      id: 2,
      name: '女'
    }],
    sex: 0,
    educationList: ["请选择学历", '博士后', '博士', '研究生', '本科', '大专', '大专以下'], //学历
    indexEducation: 0,
    region: ["", "", ""],
    dateOfBirth: '',
    nationList: ["请选择民族", '汉族', '蒙古族', '回族', '藏族', '维吾尔族', '苗族', '彝族', '壮族', '布依族', '朝鲜族', '满族', '侗族', '瑶族', '白族', '土家族', '其他'], //民族
    indexNation: 0,
    politicalAffiliationList: ["请选择政治面貌", '群众', '中国共青团团员', '中国共产党党员', '无党派民主人士', '民主党派人士'], //学历
    indexPoliticalAffiliation: 0,
  },

  onLoad: function (options) {
    var time = util.formatTime(new Date());
    this.setData({
      nowDate: time.substring(0, 10),
    })

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    if (prevPage.route == "pages/proAdd/proAdd" || prevPage.route == "pages/signInformation/signInformation") {
      app.globalData.btn = true
    } else {
      app.globalData.btn = false
    }
  },

  onShow: function () {
    this.getUserDetail()
  },
  /**
   * 获取当前用户信息
   */
  getUserDetail: function () {
    var _this = this;
    app._getLogin(siteinfo.selUserDetail, {}, function (result) {
      _this.setData(
        result.data
      );
      if (result.data.province != '' && result.data.province != null) {
        var a = 'region[' + 0 + ']'
        _this.setData({ [a]: result.data.province });
      }
      if (result.data.city != '' && result.data.city != null) {
        var b = 'region[' + 1 + ']'
        _this.setData({ [b]: result.data.city });
      }
      if (result.data.district != '' && result.data.district != null) {
        var c = 'region[' + 2 + ']'
        _this.setData({ [c]: result.data.district });
      }
      if (result.data.dateOfBirth != '' && result.data.dateOfBirth != null) {
        _this.setData({
          dateOfBirth: result.data.dateOfBirth.substring(0, 10),
        });
      }
      for (var i = 0; i < _this.data.educationList.length; i++) {
        if (result.data.education == _this.data.educationList[i]) {
          _this.setData({
            indexEducation: i
          })
          break
        }
      }
      for (var i = 0; i < _this.data.nationList.length; i++) {
        if (result.data.nation == _this.data.nationList[i]) {
          _this.setData({
            indexNation: i
          })
          break
        }
      }
      for (var i = 0; i < _this.data.politicalAffiliationList.length; i++) {
        if (result.data.politicalAffiliation == _this.data.politicalAffiliationList[i]) {
          _this.setData({
            indexPoliticalAffiliation: i
          })
          break
        }
      }
    });
  },
  //选择性别
  sexChange: function (e) {
    var _this = this;
    this.setData({
      sex: e.detail.value,
      // sex: _this.data.sexList[e.detail.value].id
    })
  },

  //选择学历
  educationChange: function (e) {
    this.setData({
      indexEducation: e.detail.value
    })
  },
  //选择民族
  nationChange: function (e) {
    this.setData({
      indexNation: e.detail.value
    })
  },
  //选择政治面貌
  politicalAffiliationChange: function (e) {
    this.setData({
      indexPoliticalAffiliation: e.detail.value
    })
  },
  //选择出生年月
  bindDateChange: function (e) {
    this.setData({
      dateOfBirth: e.detail.value
    })
  },
  //选择省市区
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  // 表单提交
  formSubmit: function (e) {
    var _this = this,
      values = e.detail.value;
    // values.sexName = _this.data.sex[values.indexSex]
    // 表单提交
    values.sex = _this.data.sex //性别
    values.education = _this.data.educationList[_this.data.indexEducation] //学历
    values.nation = _this.data.nationList[_this.data.indexNation] //民族
    values.politicalAffiliation = _this.data.politicalAffiliationList[_this.data.indexPoliticalAffiliation] //政治面貌
    values.dateOfBirth = _this.data.dateOfBirth //出生日期
    values.fileUrl = _this.data.fileUrl
    //console.log("this", _this.data)
    // 表单验证
    if (!_this.validation(values)) {
      app.showError(_this.data.error);
      return false;
    }
    app._post_form(siteinfo.editUserDetail, {
      username: values.username,
      name: values.name,
      sex: values.sex,
      // idNumber: values.idNumber,
      mobile: _this.data.mobile,
      // orgName: values.orgName,
      dateOfBirth: values.dateOfBirth,
      nation: values.nation,
      education: values.education,
      major: values.major,
      politicalAffiliation: values.politicalAffiliation,
      officialCapacity: values.officialCapacity,
      QQ: values.QQ,
      email: values.email,
      experience: values.experience,
      liveAddress: values.liveAddress,
      province: _this.data.region[0],
      city: _this.data.region[1],
      district: _this.data.region[2],
      lastEdit: 101
    }, function (res) {
      if (_this.data.cerOpenId != '' && _this.data.cerOpenId != null) {
        app.subscribeMessage(res.data.tempId)
      }
      wx.navigateBack()
      setTimeout(function () {
        wx.showToast({
          title: '修改成功'
        })
      }, 1000)
    });
  },
  /**
   * 表单验证
   */
  validation: function (values) {
    var _this = this
    if ('' == values.username) {
      this.data.error = '用户名不能为空';
      return false;
    }
    if ('' == values.name) {
      this.data.error = '真实姓名不能为空';
      return false;
    }
    // if ('' != values.mobile) {
    //   var mobile = util.regexConfig().phone;
    //   if (!mobile.test(values.mobile.trim())) {
    //     this.data.error = '请输入正确的手机号';
    //     return false;
    //   }
    // }
    // if ('' != values.idNumber) {
    //   // console.log("_this.data.idCard", _this.data.idCard)
    //   if (!util.cardId().checkIdCardNo(values.idNumber.trim())) {
    //     app.showError('请输入正确的身份证号');
    //     return false;
    //   }
    // }
    // if ('' != values.email) {
    //   var email = util.regexConfig().email;
    //   if (!email.test(values.email.trim())) {
    //     this.data.error = '请输入正确的邮箱';
    //     return false;
    //   }
    // }
    if (0 == values.sex) {
      this.data.error = '性别不能为空';
      return false;
    }

    if ('' == values.dateOfBirth || null == values.dateOfBirth) {
      this.data.error = '出生年月不能为空';
      return false;
    }
    if ('请选择民族' == values.nation) {
      this.data.error = '民族不能为空';
      return false;
    }
    if ('请选择学历' == values.education) {
      this.data.error = '学历不能为空';
      return false;
    }
    if ('' == values.major) {
      this.data.error = '专业不能为空';
      return false;
    }
    if ('请选择政治面貌' == values.politicalAffiliation) {
      this.data.error = '政治面貌不能为空';
      return false;
    }
    if ('' == values.officialCapacity) {
      this.data.error = '职务职称不能为空';
      return false;
    }
    if ('' == values.idNumber) {
      this.data.error = '身份证号不能为空';
      return false;
    }
    if ('' == values.unitName) {
      this.data.error = '所在单位名称不能为空';
      return false;
    }
    if ('' == _this.data.region[0]) {
      this.data.error = '居住省市区不能为空';
      return false;
    }
    if ('' == values.liveAddress) {
      this.data.error = '现居住详细地址不能为空';
      return false;
    }
    if ('' == values.experience) {
      this.data.error = '学习、工作经历不能为空';
      return false;
    }

    return true;
  },
  formReset: function (e) {
    var _this = this
    _this.setData({
      index: 0,
      indexCer: 0
    })
  },


  onShareAppMessage: function () {

  }
})